<?php

namespace App\Admin\Controllers\XinYou;

use App\Admin\Exports\MsgLogExport;
use App\Admin\Exports\UserProfileExport;
use App\Http\Controllers\Controller;
use App\Model\Guild\GuildAccountDaily;
use App\Model\Guild\HostAccountDaily;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;
use App\Model\GuildManage;
use App\Admin\Exports\GuildAccountDailyExport;
/**
 * 代理结算
 */
class GuildAccountDailyController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('推广员结算日报');
            $content->description('');
            $content->body($this->guild_grid());
        });
    }

    /**
     * 代理日报
     */
    protected function guild_grid()
    {

        if(Admin::user()->id == 48){
            return [];
        }

        $grid = new Grid(new GuildAccountDaily());
//        $grid = Admin::grid(GuildAccountDaily::class, function (Grid $grid) {
            $adminId = Admin::user()->id;
           if($adminId == 48 || in_array($adminId,[49,50,51])){
                $guidIds = GuildManage::getGuidIdByAdminId($adminId);
                $grid->model()->where(['flag' => 1])->whereIn('guild_manage_id',$guidIds)->orderBy('id', 'desc');
            }else{
                $grid->model()->where(['flag' => 1])->orderBy('id', 'desc');
            }
            $grid->dt('日期')->totalRow(function ($data) {
                return $this->buildRow('汇总：');
            });
            $grid->guildInfo()->guild_id('代理ID');
            $grid->guildInfo()->name('代理名');

            $grid->active_host_num('在线女用户')->sortable();
            $grid->income_host_num('收益女用户')->sortable();
            $grid->voice_income('语音收入')->totalRow(function ($data) {
                return $this->buildRow($this->t1 = $data);
            })->sortable();
            $grid->video_income('视频收入')->totalRow(function ($data) {
                return $this->buildRow($this->t2 = $data);
            })->sortable();
            $grid->msg_income('私信收入')->totalRow(function ($data) {
                return $this->buildRow($this->t3 = $data);
            });
            $grid->gift_income('礼物收入')->totalRow(function ($data) {
                return $this->buildRow($this->t4 = $data);
            })->sortable();
            $grid->invite_recharge_income('邀请充值奖励')->totalRow(function ($data) {
                return $this->buildRow($this->t5 = $data);
            })->sortable();
            $grid->ratio('当日结算比例')->display(function ($data){
                return bcmul($this->share_ratio == 0 ? env('GUILD_SHARE_PERCENT', 0.35) : $this->share_ratio,100,2).'%';
            });
            $grid->guild_share('推广预分成')->display(function ($data) {
                $baseIncome = $this->voice_income
                    + $this->video_income
                    + $this->msg_income
                    + $this->gift_income;
                $inviteIncome = $this->invite_recharge_income;
                $share = $this->share_ratio == 0 ? env('GUILD_SHARE_PERCENT', 0.35) : $this->share_ratio;
                return round($baseIncome * $share, 2);
//                return round($baseIncome * $share, 2) + round($inviteIncome * $share, 2);
            });
            // $grid->guild_share('推广预分成')->totalRow(function ($data) {
            //     $baseIncome = $this->t1 + $this->t2 + $this->t3 + $this->t4;
            //     $inviteIncome = $this->t5;
            //     $share = env('GUILD_SHARE_PERCENT', 0.35);
            //     return round($baseIncome * $share, 2) + round($inviteIncome * $share, 2);
            // })->sortable();
            //禁用导出
//            $grid->disableExport();

            $grid->disableCreateButton();
            //禁用行选择
            $grid->disableActions();


            $grid->exporter(new GuildAccountDailyExport($grid));

            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->between('dt', '日期')->date();
                });

                $filter->column(1 / 3, function ($filter) {
                    $filter->in('guildInfo.id', '所属代理')->multipleSelect('/get_guilds');
                });

            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });



            $grid->footer(function ($query)  {
                $where = [];
                $dt = request()->input('dt');
                $guildInfoId = request()->input('guildInfo.id');
                if(!empty($dt['start'])) $where[] = ['dt','>=',$dt['start']];
                if(!empty($dt['end'])) $where[]  = ['dt','<=',$dt['end']];
                if(!empty($guildInfoId)) {
                    $totalData = GuildAccountDaily::getTotal($where,$guildInfoId);
                    $total = empty($totalData) ? 0 : $totalData['voice_income'] + $totalData['video_income']
                        + $totalData['msg_income'] + $totalData['gift_income']+$totalData['invite_recharge_income'];
                    return '<table class="table custom-data-table data-table">
                    <thead>
                    <tr>
                        <th>语音收入+视频收入+私信收入+礼物收入+邀请充值奖励（金币）：' . $total . '</th>
                    </tr>
                    </thead></table>';
                }


            });
//        });
        return $grid;
    }

    function buildRow($amount = '')
    {
        return "<span class='text-danger text-bold'> {$amount} </span>";
    }

    /**
     * 女用户分成
     */
    public function host_share()
    {
        return Admin::content(function (Content $content) {
            $content->header('推广成员日报表');
            $content->description('');
            $content->body($this->host_grid());
        });
    }

    /**
     * 女用户日报
     */
    protected function host_grid()
    {
        return Admin::grid(HostAccountDaily::class, function (Grid $grid) {
            $grid->model()->orderBy('dt', 'desc')->orderBy('guild_manage_id', 'desc');
            $grid->dt('日期');
            $grid->guildInfo()->guild_id('所属推广员ID');
            $grid->guildInfo()->name('推广员名');
            $grid->binded_at('签约日期');
            $grid->hostInfo()->user_profile_id('成员ID');
            $grid->hostInfo()->nick_name('昵称');
            $grid->hostInfo()->charm_value('魅力值');
            $grid->voice_share_income('语音收入');
            $grid->video_share_income('视频收入（含福利通话）');
            $grid->msg_share_income('私信收入');
            $grid->gift_share_income('礼物收入');
            $grid->invite_recharge_income('邀请奖励收入');
            $grid->total_turnover('总收入')->display(function ($data) {
                return $this->voice_share_income + $this->video_share_income + $this->msg_share_income + $this->gift_share_income + $this->invite_recharge_income;
            });
            // $grid->host_share_income('总分成');
            $grid->disableCreateButton();
            //禁用行选择
            $grid->disableActions();
            $grid->disableRowSelector();
            // $grid->showFilterButton(false);
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->date('dt', '日期');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('guildInfo.id', '所属推广员')->select('/get_guilds');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('guildHost.host_profile_id', '成员ID');
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }
}
